'use strict';
/**
 * 推荐作业
 *
 * created by qiuwei
 * 2016年06月04日
 */

const builder = require('../filterBuilder');
const api = require('../dicAPIUrl');
const fillToMapHandler = require('../util').fillToMapHandler;

function request(requestData) {
	// 人次
	if (requestData.PV) {
		return getObjectsWithPerson(requestData);
	}
	// 人数
	else if (requestData.UV) {
		return getObjectsWithCount(requestData);
	} else
		throw new Error('在查询题库数据分析时出现未定义的配置');
}

function response(entryData, detailtData, questionData) {
	// 201：推荐—名师推荐;202：推荐—其他老师推荐;203：推荐—热门知识点;204：推荐—核心考点;205：推荐—精选专题;206：推荐—名校试卷;207：推荐—精品试卷;
	let categories = new Set();
	let toMapProxy = fillToMapHandler.bind(categories);

	let entryMap = toMapProxy(entryData);
	let detailMap = toMapProxy(detailtData);
	let questionMap = toMapProxy(questionData);

	let rn = {
		categories: Array.from(categories),
		entrySeries: [],
		detailSeries: [],
		questionSeries: [],
		// entryConversionSeries: [],
		// questionConversionSeries: []
	};

	rn.categories.forEach((item) => {
		let entryItem = parseFloat(entryMap[item]) || 0;
		rn.entrySeries.push(entryItem);

		let detailItem = parseFloat(detailMap[item]) || 0;
		rn.detailSeries.push(detailItem);

		let questionItem = parseFloat(questionMap[item]) || 0;
		rn.questionSeries.push(questionItem);

		// rn.entryConversionSeries.push(entryItem == 0 ? 0 : Math.round(detailItem / entryItem * 100));
		// rn.questionConversionSeries.push(detailItem == 0 ? 0 : Math.round(questionItem / detailItem * 100));
	});

	return rn;
}

/**
 * @private
 * 解析人次请求查询参数
 * @param  {object} requestData 请求数据
 * @return {array}             请求参数
 */
function getObjectsWithPerson(requestData) {
	// 解析常规筛选条件
	let filters = filter(requestData.condition);
	let timeUnit = requestData.currentTimeUnit;
	let optionID = requestData.currentOptionID;
	let startDate = requestData.condition.startDate;
	let endDate = requestData.condition.endDate;

	// 入口
	let entry = {
		"data": {
			"target": "action_select_question-entry_click_count",
			"data_type": "sum",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": [{
			"item": "action_select_question-entry_type",
			"type": "=",
			"value": optionID //201：推荐—名师推荐;202：推荐—其他老师推荐;203：推荐—热门知识点;204：推荐—核心考点;205：推荐—精选专题;206：推荐—名校试卷;207：推荐—精品试卷;
		}, {
			"item": "action_select_question-day",
			"type": ">=",
			"value": startDate
		}, {
			"item": "action_select_question-day",
			"type": "<=",
			"value": endDate
		}].concat(filters)
	};

	// 详情
	let detail = {
		"data": {
			"target": "action_select_question-entry_ctt_click_count",
			"data_type": "sum",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": [{
			"item": "action_select_question-entry_type",
			"type": "=",
			"value": optionID //201：推荐—名师推荐;202：推荐—其他老师推荐;203：推荐—热门知识点;204：推荐—核心考点;205：推荐—精选专题;206：推荐—名校试卷;207：推荐—精品试卷;
		}, {
			"item": "action_select_question-day",
			"type": ">=",
			"value": startDate
		}, {
			"item": "action_select_question-day",
			"type": "<=",
			"value": endDate
		}].concat(filters)
	};

	// 用题人次
	let person = {
		"data": {
			"target": "question_usage-homework_num",
			"data_type": "sum",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": [{
			"item": "question_usage-entry_type",
			"type": "=",
			"value": optionID //201：推荐—名师推荐;202：推荐—其他老师推荐;203：推荐—热门知识点;204：推荐—核心考点;205：推荐—精选专题;206：推荐—名校试卷;207：推荐—精品试卷;
		}, {
			"item": "question_usage-action_type",
			"type": "=",
			"value": 1
		}, {
			"item": "question_usage-day",
			"type": ">=",
			"value": startDate
		}, {
			"item": "question_usage-day",
			"type": "<=",
			"value": endDate
		}].concat(filters)
	};

	return [entry, detail, person];
}

/**
 * @private
 * 解析题次请求查询参数
 * @param  {object} requestData 请求数据
 * @return {Array}             请求参数
 */
function getObjectsWithCount(requestData) {
	// 解析常规筛选条件
	let filters = filter(requestData.condition);
	let timeUnit = requestData.currentTimeUnit;
	let optionID = requestData.currentOptionID;
	let startDate = requestData.condition.startDate;
	let endDate = requestData.condition.endDate;

	let entry = {
		"data": {
			"target": "action_select_question-teacher_id",
			"data_type": "count",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": [{
			"item": "action_select_question-entry_type",
			"type": "=",
			"value": optionID //201：推荐—名师推荐;202：推荐—其他老师推荐;203：推荐—热门知识点;204：推荐—核心考点;205：推荐—精选专题;206：推荐—名校试卷;207：推荐—精品试卷;
		}, {
			"item": "action_select_question-entry_click_count",
			"type": ">",
			"value": 0
		}, {
			"item": "action_select_question-day",
			"type": ">=",
			"value": startDate
		}, {
			"item": "action_select_question-day",
			"type": "<=",
			"value": endDate
		}].concat(filters)
	}

	let detail = {
		"data": {
			"target": "action_select_question-teacher_id",
			"data_type": "count",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": [{
			"item": "action_select_question-entry_type",
			"type": "=",
			"value": optionID //201：推荐—名师推荐;202：推荐—其他老师推荐;203：推荐—热门知识点;204：推荐—核心考点;205：推荐—精选专题;206：推荐—名校试卷;207：推荐—精品试卷;
		}, {
			"item": "action_select_question-entry_ctt_click_count",
			"type": ">",
			"value": 0
		}, {
			"item": "action_select_question-day",
			"type": ">=",
			"value": startDate
		}, {
			"item": "action_select_question-day",
			"type": "<=",
			"value": endDate
		}].concat(filters)
	};

	let question = {
		"data": {
			"target": "question_usage-teacher_id",
			"data_type": "count",
			"type": "normal"
		},
		"time_unit": timeUnit,
		"multi_unit": "",
		"filter": [{
			"item": "question_usage-entry_type",
			"type": "=",
			"value": optionID //201：推荐—名师推荐;202：推荐—其他老师推荐;203：推荐—热门知识点;204：推荐—核心考点;205：推荐—精选专题;206：推荐—名校试卷;207：推荐—精品试卷;
		}, {
			"item": "question_usage-action_type",
			"type": "=",
			"value": 1
		}, {
			"item": "question_usage-day",
			"type": ">=",
			"value": startDate
		}, {
			"item": "question_usage-day",
			"type": "<=",
			"value": endDate
		}].concat(filters)
	};

	return [entry, detail, question];
}

/**
 * @private
 * 解析常规筛选条件
 * @param  {object} condition 筛选条件对象
 * @return {array}            条件数组
 */
function filter(condition) {
	let filters = [];

	if (condition.province && condition.province != builder.NIL) {
		filters.push(builder.append('teacher-province_id', '=', condition.province));
	}
	if (condition.city && condition.city != builder.NIL) {
		filters.push(builder.append('teacher-city_id', '=', condition.city));
	}
	if (condition.county && condition.county != builder.NIL) {
		filters.push(builder.append('teacher-county_id', '=', condition.county));
	}
	if (condition.school && condition.school != builder.NIL) {
		filters.push(builder.append('teacher-school_id', '=', condition.school));
	}
	if (condition.grade[0] != builder.NIL) {
		filters.push(builder.append('teacher-grade', 'in', condition.grade.join(',')));
	}
	if (condition.subject[0] != builder.NIL) {
		filters.push(builder.append('teacher-subject', 'in', condition.subject.join(',')));
	}
	if (condition.version != '0') {
		filters.push(builder.append('teacher-version', '=', condition.version));
	}
	if (condition.deviceType[0] != builder.NIL) {
		filters.push(builder.append('teacher-source', 'in', condition.deviceType.join(',')));
	}

	// filters.push(builder.append('action_select_question-day', '>=', condition.startDate));
	// filters.push(builder.append('action_select_question-day', '<=', condition.endDate));
	filters.push(builder.append('teacher-student_count', '>=', condition.userFeature));

	return filters;
}

module.exports = {
	API_URL: api.API_PACKAGE_DATA,
	request,
	response
}